define(function(require, exports, module) {
	
	require("layout");
	require("ztree");
	
	module.exports = function() {
		var MENU_TYPES = "MENU-TYPES";
		var zTree = null;
		var perms = {
			add:false,
			edit:false,
			remove:false
		}
		return {
			data: {
				activeMenuName : '',
				types:[]
			},
			methods: {
				init: function(data) {
					var me = this;
					var params = {
						add:"sys:menu:manager:add",
						edit:"sys:menu:manager:edit",
						remove:"sys:menu:manager:remove"
					};
					//获取权限
					$.perms(params, function(isOk, data) {
						if(isOk) {
							$.extend(perms, data);
						}
						me.initTypes(function() {
							if(me.types.length > 0) {
								me.onTypeChange(me.types[0].value);
							}
						});
					});
					return false;
				},
				//初始化左侧菜单类型
				initTypes:function(complete) {
					var me = this;
					$.dict({values:MENU_TYPES, children:true}, function(isOk, dict) {
						if(isOk) {
							me.types = dict.children;
						}
						if($.isFunction(complete)) {
							complete();
						}
					});
				},
				onTypeChange: function(value) {
					if(this.activeMenuName != value) {
						this.loading();
						this.activeMenuName = value;
						this.$nextTick(function() {
							this.$refs.typeMenu.updateActiveName();
						});
						if(zTree) {
							this.loadTree();
						} else {
							this.initTree();
						}
					}
				},
				//初始化权限树
				initTree:function() {
					var me = this;
					var ul = $(this.$el).find("ul.ztree");
					var setting = {
						data: {
							key: {
								icon:"iconName"
							},
							simpleData: {
								enable: true,
								pIdKey: "type"
							}
						},
						async : {
							enable : true,
							url:"/api/menu/tree",
							dataType:"json",
							otherParam:{
								type:me.activeMenuName
							},
							dataFilter:function(treeId, parentNode, responseData) {
								var data = responseData.message;
								data.name = $.i18n.prop(data.i18n);
								return [data];
							}
						},
						callback : {
							onAsyncSuccess: function(event, treeId, treeNode, data) {
								me.loading("close");
							}
						},
						view: {
							addHoverDom:function(treeId, treeNode) {
								var container = $("#" + treeNode.tId + "_span");
								var buttons = container.children("span");
								if (treeNode.editNameFlag || buttons.length > 0) return;

								buttons = $("<span></span>").appendTo(container);
								if(perms.add) {
									var addButton = $('<span class="button add"></span>').appendTo(buttons);
									addButton.attr("title", $.i18n.prop("info.add"));
									addButton.bind("click.treeItem", function() {
										me.showAdd(treeNode);
									});
								}
								if(treeNode.level == 0) {
									return;
								}
								if(perms.edit) {
									var editButton = $('<span class="button edit"></span>').appendTo(buttons);
									editButton.attr("title", $.i18n.prop("info.edit"));
									editButton.bind("click.treeItem", function() {
										me.showEdit(treeNode);
									});
								}
								if(treeNode.children && treeNode.children.length > 0) {
									return;
								}
								if(perms.remove) {
									var removeButton = $('<span class="button remove"></span>').appendTo(buttons);
									removeButton.attr("title", $.i18n.prop("info.remove"));
									removeButton.bind("click.treeItem", function() {
										me.confirmRemove(treeNode);
									});
								}
							},
							removeHoverDom:function(treeId, treeNode) {
								var container = $("#" + treeNode.tId + "_span");
								var buttons = container.children("span");
								buttons.find("a").unbind("click.treeItem");
								buttons.remove();
							}
						}
					};
					zTree = $.fn.zTree.init(ul, setting);
				},
				loadTree: function() {
					zTree.destroy();
					zTree = null;
					this.initTree();
				},
				showAdd: function(treeNode) {
					var extraParams = {
						node:treeNode,
						addNodes:function(parent, nodes) {
							zTree.addNodes(parent, nodes);
						}
					};
					$.window({
						router: "sys/menu/add",
						shade: 0.3
					}, extraParams);
				},
				showEdit: function(treeNode) {
					var extraParams = {
						node:treeNode,
						updateNode:function(node) {zTree.updateNode(node);}
					};
					$.window({
						router: "sys/menu/edit",
						shade: 0.3
					}, extraParams);
				},
				confirmRemove: function(treeNode) {
					$.confirm($.i18n.prop("sys.remove.confirm"), function(isOk) {
						if(isOk) {
							var _this = this;
							$.post("/api/menu/remove", {id:treeNode.id}, function(data) {
								if(data.success) {
									zTree.removeNode(treeNode);
								}
								_this.remove();
							}, "json");
						} else {
							this.remove();
						}
					}, true);
				},
				destroy: function() {
					if(zTree) {
						zTree.destroy();
						zTree = null;
					}
				}
			}
		}
	}
});